#!/bin/bash

# 微信公众号文章生成工具 - 快速启动脚本

echo "==================================="
echo "微信公众号文章生成工具"
echo "==================================="

# 检查Python版本
python_version=$(python --version 2>&1 | grep -o '[0-9]\+\.[0-9]\+')
required_version="3.8"

if [ "$(printf '%s\n' "$required_version" "$python_version" | sort -V | head -n1)" = "$required_version" ]; then
    echo "✓ Python版本检查通过: $python_version"
else
    echo "✗ Python版本过低，需要3.8+，当前版本: $python_version"
    exit 1
fi

# 检查.env文件
if [ ! -f ".env" ]; then
    echo "✗ .env文件不存在，请先配置环境变量"
    echo "请参考README.md中的配置说明"
    exit 1
fi

echo "✓ 环境配置文件检查通过"

# 安装依赖
echo "正在检查Python依赖..."
if [ ! -d "venv" ]; then
    echo "创建虚拟环境..."
    python -m venv venv
fi

# 激活虚拟环境
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
    source venv/Scripts/activate
else
    source venv/bin/activate
fi

echo "安装/更新依赖包..."
pip install --upgrade pip
pip install -r requirements.txt

# 创建必要目录
mkdir -p logs
echo "✓ 创建日志目录"

# 初始化数据库
echo "初始化数据库和示例数据..."
python init_data.py

# 启动应用
echo "启动应用服务..."
echo "应用将在 http://localhost:8000 启动"
echo "API文档: http://localhost:8000/docs"
echo "系统状态: http://localhost:8000/health"
echo ""
echo "按 Ctrl+C 停止服务"
echo "==================================="

python run.py